/**
 * 
 */
package org.istt.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.jws.WebService;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.istt.domain.Category;
import org.istt.domain.File;
import org.istt.domain.FileAttachment;
import org.istt.domain.FileBoSungCCHN;
import org.istt.domain.FileCapCCHN;
import org.istt.domain.FileCapCCMGBDS;
import org.istt.domain.FileCapLaiCCHN;
import org.istt.domain.FileCapPhepXayDung;
import org.istt.domain.FileDieuChinhGPXD;
import org.istt.domain.FileGiaHanCPXD;
import org.istt.model.CategoryBean;
import org.istt.model.FileServiceBean;
import org.istt.service.CategoryManager;
import org.istt.service.FileBoSungCCHNManager;
import org.istt.service.FileCapCCHNManager;
import org.istt.service.FileCapCCMGBDSManager;
import org.istt.service.FileCapGPXDManager;
import org.istt.service.FileCapLaiCCHNManager;
import org.istt.service.FileDieuChinhGPXDManager;
import org.istt.service.FileGiaHanGPXDManager;
import org.istt.service.FileManager;
import org.istt.service.FileServiceManager;
import org.istt.web.WebConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;


/**
 * 
 * @author ndtho
 *
 */
@Service("fileServiceManager")
@WebService(serviceName = "FileService", endpointInterface = "org.istt.service.FileServiceManager")
public class FileServiceManagerImpl implements FileServiceManager {
	
	FileManager fileMgr;
	CategoryManager categoryMgr;
	
	private final Log log = LogFactory.getLog(FileServiceManagerImpl.class);
	FileCapGPXDManager capMoiGPXDMgr;
	FileGiaHanGPXDManager giaHanGPXDMgr;
	FileDieuChinhGPXDManager dieuChinhGPXDMgr;

	FileCapCCHNManager capMoiCCHNMgr;
	FileCapLaiCCHNManager capLaiCCHNMgr;
	FileBoSungCCHNManager boSungCCHNMgr;
	FileCapCCMGBDSManager capCCMGBDSMgr;
	private String ip;
	private String port;


	@Autowired
	public void setCapCCMGBDSMgr(FileCapCCMGBDSManager capCCMGBDSMgr) {
		this.capCCMGBDSMgr = capCCMGBDSMgr;
	}

	@Autowired
	public void setBoSungCCHNMgr(FileBoSungCCHNManager boSungCCHNMgr) {
		this.boSungCCHNMgr = boSungCCHNMgr;
	}

	@Autowired
	public void setCapMoiGPXDMgr(FileCapGPXDManager capMoiGPXDMgr) {
		this.capMoiGPXDMgr = capMoiGPXDMgr;
	}

	@Autowired
	public void setGiaHanGPXDMgr(FileGiaHanGPXDManager giaHanGPXDMgr) {
		this.giaHanGPXDMgr = giaHanGPXDMgr;
	}

	@Autowired
	public void setDieuChinhGPXDMgr(FileDieuChinhGPXDManager dieuChinhGPXDMgr) {
		this.dieuChinhGPXDMgr = dieuChinhGPXDMgr;
	}

	@Autowired
	public void setCapMoiCCHNMgr(FileCapCCHNManager capMoiCCHNMgr) {
		this.capMoiCCHNMgr = capMoiCCHNMgr;
	}

	@Autowired
	public void setCapLaiCCHNMgr(FileCapLaiCCHNManager capLaiCCHNMgr) {
		this.capLaiCCHNMgr = capLaiCCHNMgr;
	}

	@Autowired
	public void setConstructionPermitMgr( FileCapGPXDManager constructionPermitMgr) {
		this.capMoiGPXDMgr = constructionPermitMgr;
	}

	@Autowired
	public void setFileMgr(FileManager fileMgr) {
		this.fileMgr = fileMgr;
	}
	
	@Autowired
	public void setCategoryMgr(CategoryManager categoryMgr) {
		this.categoryMgr = categoryMgr;
	}

	@Override
	public List<FileServiceBean> searchFile(String pageNum, String fileCode, String sender,String categoryCode, String fromDate, String toDate) {
		// TODO Auto-generated method stub
		DateFormat dateFormat = new SimpleDateFormat(WebConstants.DATE_FORMAT);
		
		List<FileServiceBean> result = new ArrayList<FileServiceBean>();
//		if((fileCode!=null && !"".equals(fileCode)) || (sender!=null && !"".equals(sender))){
//			List<File> lstfile = fileMgr.getFilesByNameAndCode(fileCode, sender);
			Long cateId = (long) 0;
			Date fDate = null;
			Date tDate = null;
			
			if(StringUtils.isNotEmpty(categoryCode)) cateId = Long.parseLong(categoryCode);
			
			if(StringUtils.isNotEmpty(toDate) && StringUtils.isNotEmpty(fromDate) ){
				try {
					fDate = StringUtils.isEmpty(fromDate) ? null : DateUtils.parseDate(
							fromDate, new String[] { "dd/MM/yyyy"});
					
					tDate = StringUtils.isEmpty(toDate) ? null : DateUtils.parseDate(
							toDate, new String[] { "dd/MM/yyyy" });
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
				
//			List<File> lstfile = fileMgr.searchWithoutPagination(new Long(0), new Long(0), new Long(0), cateId, fDate, tDate, fileCode, sender);
			int page = 0;
			if(StringUtils.isNotEmpty(pageNum)) page = Integer.parseInt(pageNum); 
			List<File> lstfile = fileMgr.searchFile(cateId, fDate, tDate, fileCode, sender, page);
			
			if(lstfile!=null){
				for (File file : lstfile) {
					FileServiceBean fileObj = new FileServiceBean();
					fileObj.setFileName(file.getSender());
					fileObj.setFileCode(file.getId());
					fileObj.setInputDate(dateFormat.format(file.getInputDate().toDate()));
					fileObj.setOutputDate(dateFormat.format(file.getOutputDate().toDate()));
					fileObj.setStatus(file.getFileStatus().getStatus());
					
					result.add(fileObj);
				}
			}
//		}
			
		return result;
	}


	/***
	 * CAP GPXD
	 */
	@Override
	public ArrayList<String> submitForm_Cap_GPXD(String contactName, String email,	String categoryType, String investor, String representative,
			String position, String contactAdd, String buildingNo,
			String street, String province, String district, String city,
			String phone, String lotNumber, String lotArea, String lotAdd,
			String lotStreet, String lotProvince, String lotDistrict,
			String lotCity, String lotOrigin, String contentProjectType,
			String contentProjectLevel, String contentArea,
			String contentTotalAreas, String contentBuildingHeight,
			String contentNumberOfFloor, String designer, String designerAdd,
			String designerPhone, String designReviewer, String reviewerPhone,
			String reviewerAdd, String reviewerLicenseNo,
			String reviewerLicenseDate, String demolitionSolution,
			String completingTime, String[] fileAttachments, String fileCode) {
		
		ArrayList<String> result = new ArrayList<String>();
		
		FileCapPhepXayDung constPermit = new FileCapPhepXayDung();
		constPermit.setFileCode(fileCode);
		
		constPermit.setThu_tuc(WebConstants.SUBMIT_ONLINE_CAP_GPXD);
		constPermit.setNgay_nop(new Date());
		constPermit.setReceived(false);
		
		constPermit.setEmail(email);
		constPermit.setTen_lien_he(contactName);
		constPermit.setDien_thoai(phone);
		constPermit.setChu_dau_tu(investor);
		constPermit.setDia_chi_lien_he(contactAdd);
		constPermit.setDonvi_dia_chi(designerAdd);
		constPermit.setChuc_vu(position);
		constPermit.setDonvi_dien_thoai(designerPhone);
		constPermit.setDonvi_tk(designer);
		constPermit.setDuong(street);
		constPermit.setLoai_thu_tuc(categoryType);
		constPermit.setLodat_dia_chi(lotAdd);
		constPermit.setLodat_dien_tich(lotArea);
		constPermit.setLodat_duong(lotStreet);
		constPermit.setLodat_nguon_goc(lotOrigin);
		constPermit.setLodat_phuong(lotProvince);
		constPermit.setLodat_quan(lotDistrict);
		constPermit.setLodat_so(lotNumber);
		constPermit.setLodat_tp(lotCity);
		constPermit.setNguoi_dai_dien(representative);
		constPermit.setNoidung_cap_ct(contentProjectLevel);
		constPermit.setNoidung_chieucao(contentBuildingHeight);
		constPermit.setNoidung_dt_tang(contentArea);
		constPermit.setNoidung_loai_ct(contentProjectType);
		constPermit.setNoidung_so_tang(contentNumberOfFloor);
		constPermit.setNoidung_tong_dt_san(contentTotalAreas);
		constPermit.setPhuong(province);
		constPermit.setPhuong_an_pha_do(demolitionSolution);
		constPermit.setQuan(district);
		constPermit.setSo_nha(buildingNo);
		constPermit.setThamdinh_dia_chi(reviewerAdd);
		constPermit.setThamdinh_dien_thoai(reviewerPhone);
		constPermit.setThamdinh_giay_phep(reviewerLicenseNo);
		constPermit.setThamdinh_ngay_cap(reviewerLicenseDate);
		constPermit.setThamdinh_tk(designReviewer);
		constPermit.setThanh_pho(city);
		constPermit.setThoi_gian_hoan_thanh(completingTime);
		
		Set<FileAttachment> lstFile =  new HashSet<FileAttachment>(0);
		if(fileAttachments!=null){
			for (int i = 0; i < fileAttachments.length; i++) {
				String receivedFile = fileAttachments[i];
				if(receivedFile!=null && !"".equals(receivedFile)){
					
					String fileName = receivedFile.substring(receivedFile.lastIndexOf("/")+1);
					FileAttachment attachfile = new FileAttachment();
					attachfile.setFileName(fileName);
					attachfile.setUrl(fileAttachments[i]);
					attachfile.setFileCapPhepXayDung(constPermit);
					lstFile.add(attachfile);
				}
			}
			constPermit.setFileAttachments(lstFile);
		}
		
		
		FileCapPhepXayDung insertedObj = null;
		try {
			
			insertedObj = capMoiGPXDMgr.insert(constPermit);
			
			result.add("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			result.add("FAILURE");
		}
		FileServiceManagerImpl server = (FileServiceManagerImpl) new ClassPathXmlApplicationContext("server.xml").getBean("serverInfo");
		if(insertedObj!=null){
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!getDocument.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_GPXD_CM);
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!exportNotify.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_GPXD_CM);
		}
		
		return result;
	}

	private FileAttachment createNewAttachFile(String receivedFile){
		FileAttachment attachfile = new FileAttachment();
		
		String fileName = receivedFile.substring(receivedFile.lastIndexOf("/")+1);
		attachfile.setFileName(fileName);
		attachfile.setUrl(receivedFile);
		
		return attachfile;
	}
	/***
	 * GIA HAN GPXD
	 */
	@Override
	public ArrayList<String> submitForm_GIAHAN_GPXD(String contactName, String email,
			String fullname, String idcardNumber, String idcardUnit,
			String idcardDate, String address, String buildingNo,
			String street, String province, String district, String phone,
			String projectOwner, String prjAdd, String prjStreet,
			String prjProvince, String prjDistrict, String oldProvider,
			String conPermitNumber, String conPermitDate, String reasonRenew,String[] fileAttachments, String fileCode) {
		
		ArrayList<String> result = new ArrayList<String>();
		FileGiaHanCPXD fileObj = new FileGiaHanCPXD();
		Set<FileAttachment> lstFile =  new HashSet<FileAttachment>(0);
		if(fileAttachments!=null){
			for (int i = 0; i < fileAttachments.length; i++) {
				String receivedFile = fileAttachments[i];
				if(receivedFile!=null && !"".equals(receivedFile)){
					
					FileAttachment attachfile = createNewAttachFile(receivedFile);
					attachfile.setFileGiaHanCPXD(fileObj);
					lstFile.add(attachfile);
				}
			}
			fileObj.setFileAttachments(lstFile);
		}
		
		fileObj.setFileCode(fileCode);
		
		fileObj.setThu_tuc(WebConstants.SUBMIT_ONLINE_GIAHAN_GPXD);
		fileObj.setNgay_nop(new Date());
		fileObj.setReceived(false);
		
		fileObj.setChu_cong_trinh(projectOwner);
		fileObj.setCmnd_donvi_cap(idcardUnit);
		fileObj.setCmnd_ngay_cap(idcardDate);
		fileObj.setCmnd_so(idcardNumber);
		fileObj.setCt_duong(prjStreet);
		fileObj.setCt_phuong(prjProvince);
		fileObj.setCt_quan(prjDistrict);
		fileObj.setDonvi_cap(oldProvider);
		fileObj.setCt_so(prjAdd);
		fileObj.setDia_chi(address);
		fileObj.setDien_thoai(phone);
		fileObj.setDuong(street);
		fileObj.setEmail(email);
		fileObj.setGpxd_ly_do_gia_han(reasonRenew);
		fileObj.setGpxd_ngay_cap(conPermitDate);
		fileObj.setGpxd_so(conPermitNumber);
		fileObj.setHo_ten(fullname);
		fileObj.setPhuong(province);
		fileObj.setQuan(district);
		fileObj.setSo_nha(buildingNo);
		fileObj.setTen_lien_he(contactName);
		
		
		FileGiaHanCPXD insertedObj = null;
		try {
			
			insertedObj = giaHanGPXDMgr.insert(fileObj);
			
			result.add("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			result.add("FAILURE");
		}
		FileServiceManagerImpl server = (FileServiceManagerImpl) new ClassPathXmlApplicationContext("server.xml").getBean("serverInfo");
		if(insertedObj!=null){
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!getDocument.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_GPXD_GH);
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!exportNotify.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_GPXD_GH);
		}
		
		return result;
	}

	/***
	 * DIEU CHINH GPXD
	 */
	@Override
	public ArrayList<String> submitForm_DIEUCHINH_GPXD(String contactName, String email,
			String fullname, String idcardNumber, String idcardUnit,
			String idcardDate, String address, String buildingNo,
			String street, String province, String district, String phone,
			String prjOwner, String prjAdd, String prjStreet,
			String prjProvince, String prjDistrict, String oldConPermitProvider,
			String conPermitNumber,String conPermitDate, String conPermitRenewNo
			 ,String adjustContent, String completingTime,String[] fileAttachments, String fileCode) {
		ArrayList<String> result = new ArrayList<String>();
		FileDieuChinhGPXD fileObj = new FileDieuChinhGPXD();
		fileObj.setFileCode(fileCode);
		Set<FileAttachment> lstFile =  new HashSet<FileAttachment>(0);
		if(fileAttachments!=null){
			for (int i = 0; i < fileAttachments.length; i++) {
				String receivedFile = fileAttachments[i];
				if(receivedFile!=null && !"".equals(receivedFile)){
					
					FileAttachment attachfile = createNewAttachFile(receivedFile);
					attachfile.setFileDieuChinhGPXD(fileObj);
					lstFile.add(attachfile);
				}
			}
			fileObj.setFileAttachments(lstFile);
		}
		
		fileObj.setThu_tuc(WebConstants.SUBMIT_ONLINE_DIEUCHINH_GPXD);
		fileObj.setNgay_nop(new Date());
		fileObj.setReceived(false);
		
		fileObj.setChu_cong_trinh(prjOwner);
		fileObj.setCmnd_donvi_cap(idcardUnit);
		fileObj.setCmnd_ngay_cap(idcardDate);
		fileObj.setCmnd_so(idcardNumber);
		fileObj.setCt_duong(buildingNo);
		fileObj.setCt_phuong(prjProvince);
		fileObj.setCt_quan(prjDistrict);
		fileObj.setCt_so(prjAdd);
		fileObj.setDia_chi(address);
		fileObj.setDien_thoai(phone);
		fileObj.setDuong(street);
		fileObj.setEmail(email);
		fileObj.setGpxd_cu_ngay_cap(conPermitDate);
		fileObj.setGpxd_cu_noi_cap(oldConPermitProvider);
		fileObj.setGpxd_cu_ngay_cap(conPermitDate);
		fileObj.setGpxd_moi_so(conPermitRenewNo);
		fileObj.setHo_ten(fullname);
		fileObj.setPhuong(province);
		fileObj.setQuan(district);
		fileObj.setSo_nha(address);
		fileObj.setTen_lien_he(contactName);
		fileObj.setNoi_dung_dieu_chinh(adjustContent);
		fileObj.setThoi_gian_hoan_thanh(completingTime);

		
		FileDieuChinhGPXD insertedObj = null;
		try {
			
			insertedObj = dieuChinhGPXDMgr.insert(fileObj);
			
			result.add("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			result.add("FAILURE");
		}
		FileServiceManagerImpl server = (FileServiceManagerImpl) new ClassPathXmlApplicationContext("server.xml").getBean("serverInfo");
		if(insertedObj!=null){
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!getDocument.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_GPXD_DC);
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!exportNotify.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_GPXD_DC);
		}
		
		return result;
	}

	/***
	 * CAP CCHN
	 */
	@Override
	public ArrayList<String> submitForm_CM_CCHN(String contactName, String email, String subject,
			String fullname, String sex, String birthday,
			String placeOfBirth, String national, String idcardNumber,
			String idcardUnit, String idcardDate, String address,
			String education, String certificate, String experienceYear,
			String designConstructionPlanning, String designArchitectural,
			String constructionSurvey, String designProject,
			String leaderOfProjects, String designOrImplement, 
			String content,String[] fileAttachments, String fileCode) {
		
		ArrayList<String> result = new ArrayList<String>();
		FileCapCCHN fileObj = new FileCapCCHN();
		fileObj.setFileCode(fileCode);
		
		Set<FileAttachment> lstFile =  new HashSet<FileAttachment>(0);
		if(fileAttachments!=null){
			for (int i = 0; i < fileAttachments.length; i++) {
				String receivedFile = fileAttachments[i];
				if(receivedFile!=null && !"".equals(receivedFile)){
					
					FileAttachment attachfile = createNewAttachFile(receivedFile);
					attachfile.setFileCapCCHN(fileObj);
					lstFile.add(attachfile);
				}
			}
			fileObj.setFileAttachments(lstFile);
		}
		
		fileObj.setThu_tuc(WebConstants.SUBMIT_ONLINE_CAP_CCHN);
		fileObj.setNgay_nop(new Date());
		fileObj.setReceived(false);
		
		fileObj.setCmnd(idcardNumber);
		fileObj.setCmnd_ngay_cap(idcardDate);
		fileObj.setCmnd_noi_cap(idcardUnit);
		fileObj.setContent(content);
		fileObj.setDiachi_thuongtru(address);
		fileObj.setEmail(email);
		fileObj.setGioi_tinh(sex);
		fileObj.setHo_ten(fullname);
		fileObj.setKn_cnct(leaderOfProjects);
		fileObj.setKn_da_tkct(designOrImplement);
		fileObj.setKn_ksxd(constructionSurvey);
		fileObj.setKn_tk(designProject);
		fileObj.setKn_tk_ktct(designArchitectural);
		fileObj.setKn_tk_qhxd(designConstructionPlanning);
		fileObj.setNam_kinh_nghiem(experienceYear);
		fileObj.setNgay_sinh(birthday);
		fileObj.setNoi_sinh(placeOfBirth);
		fileObj.setQuoc_tich(national);
		fileObj.setSubject(subject);
		fileObj.setTen_lien_he(contactName);
		fileObj.setTrinh_do(education);
		fileObj.setVan_bang(certificate);
		
		FileCapCCHN insertedObj = null;
		try {
			
			insertedObj = capMoiCCHNMgr.insert(fileObj);
			
			result.add("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			result.add("FAILURE");
		}
		FileServiceManagerImpl server = (FileServiceManagerImpl) new ClassPathXmlApplicationContext("server.xml").getBean("serverInfo");
		if(insertedObj!=null){
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!getDocument.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCHN_CM);
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!exportNotify.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCHN_CM);
		}
		
		return result;
	}

	@Override
	public ArrayList<String> submitForm_CL_CCHN(String contactName, String email,String subject,
			String fullname, String sex, String birthday, String placeOfBirth,
			String national, String idcardNumber, String idcardUnit,
			String idcardDate, String address, String level,String diploma,
			String certificate, String contentOfOldCer, String dateOfOldCer,
			String contentOfNewCer, String reasonOfNewCer,String[] fileAttachments, String fileCode) {
		
		// TODO Auto-generated method stub
		ArrayList<String> result = new ArrayList<String>();
		FileCapLaiCCHN fileObj = new FileCapLaiCCHN();
		fileObj.setFileCode(fileCode);
		Set<FileAttachment> lstFile =  new HashSet<FileAttachment>(0);
		if(fileAttachments!=null){
			for (int i = 0; i < fileAttachments.length; i++) {
				String receivedFile = fileAttachments[i];
				if(receivedFile!=null && !"".equals(receivedFile)){
					
					FileAttachment attachfile = createNewAttachFile(receivedFile);
					attachfile.setFileCapLaiCCHN(fileObj);
					lstFile.add(attachfile);
				}
			}
			fileObj.setFileAttachments(lstFile);
		}
		fileObj.setNgay_nop(new Date());
		fileObj.setThu_tuc(WebConstants.SUBMIT_ONLINE_CAPLAI_CCHN);
		fileObj.setReceived(false);
		
		fileObj.setAddress(address);
		fileObj.setLevel(level);
		fileObj.setDiploma(diploma);
		fileObj.setCertificate(certificate);
		fileObj.setContactName(contactName);
		fileObj.setDate_of_birth(birthday);
		fileObj.setEmail(email);
		fileObj.setFullname(fullname);
		fileObj.setIdcard(idcardNumber);
		fileObj.setIdcard_date(idcardDate);
		fileObj.setIdcard_location(idcardUnit);
		fileObj.setNationality(national);
		fileObj.setNew_cer_content(contentOfNewCer);
		fileObj.setNgay_nop(new Date());
		fileObj.setOld_cer_content(contentOfOldCer);
		fileObj.setOld_cer_date(dateOfOldCer);
		fileObj.setPlace_of_birth(placeOfBirth);
		fileObj.setReason_renew_cer(reasonOfNewCer);
		fileObj.setSex(sex);
		fileObj.setSubject(subject);
		
		FileCapLaiCCHN insertedObj = null;
		try {
			
			insertedObj = capLaiCCHNMgr.insert(fileObj);
			
			result.add("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			result.add("FAILURE");
		}
		FileServiceManagerImpl server = (FileServiceManagerImpl) new ClassPathXmlApplicationContext("server.xml").getBean("serverInfo");
		if(insertedObj!=null){
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!getDocument.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCHN_CL);
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!exportNotify.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCHN_CL);
		}
		
		
		return result;
	}

	@Override
	public ArrayList<String> submitForm_BS_CCHN(String contactName, String email,String subject,
			String fullname, String sex, String birthday, String placeOfBirth,
			String national, String idcardNumber, String idcardDate, String idcardUnit,
			String address, String education,
			String certificate, String experienceYear,
			String designConstructionPlanning, String designArchitectural,
			String constructionSurvey, String designProject,
			String leaderOfProjects, String designOrImplement, 
			String content,String[] fileAttachments, String fileCode) {
		
		ArrayList<String> result = new ArrayList<String>();
		
		FileBoSungCCHN fileObj = new FileBoSungCCHN();
		fileObj.setFileCode(fileCode);
		Set<FileAttachment> lstFile =  new HashSet<FileAttachment>(0);
		if(fileAttachments!=null){
			for (int i = 0; i < fileAttachments.length; i++) {
				String receivedFile = fileAttachments[i];
				if(receivedFile!=null && !"".equals(receivedFile)){
					
					FileAttachment attachfile = createNewAttachFile(receivedFile);
					attachfile.setFileBoSungCCHN(fileObj);
					lstFile.add(attachfile);
				}
			}
			fileObj.setFileAttachments(lstFile);
		}
		
		fileObj.setThu_tuc(WebConstants.SUBMIT_ONLINE_BOSUNG_CCHN);
		fileObj.setNgay_nop(new Date());
		fileObj.setReceived(false);
		
		fileObj.setCmnd(idcardNumber);
		fileObj.setCmnd_ngay_cap(idcardDate);
		fileObj.setCmnd_noi_cap(idcardUnit);
		fileObj.setContent(content);
		fileObj.setDiachi_thuongtru(address);
		fileObj.setEmail(email);
		fileObj.setGioi_tinh(sex);
		fileObj.setHo_ten(fullname);
		fileObj.setKn_cnct(leaderOfProjects);
		fileObj.setKn_da_tkct(designOrImplement);
		fileObj.setKn_ksxd(constructionSurvey);
		fileObj.setKn_tk(designProject);
		fileObj.setKn_tk_ktct(designArchitectural);
		fileObj.setKn_tk_qhxd(designConstructionPlanning);
		fileObj.setNam_kinh_nghiem(experienceYear);
		fileObj.setNgay_sinh(birthday);
		fileObj.setNoi_sinh(placeOfBirth);
		fileObj.setQuoc_tich(national);
		fileObj.setTen_lien_he(contactName);
		fileObj.setVan_bang(certificate);
		fileObj.setSubject(subject);
		
		FileBoSungCCHN insertedObj = null;
		try {
			
			insertedObj = boSungCCHNMgr.insert(fileObj);
			result.add("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			result.add("FAILURE");
		}
		FileServiceManagerImpl server = (FileServiceManagerImpl) new ClassPathXmlApplicationContext("server.xml").getBean("serverInfo");
		if(insertedObj!=null){
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!getDocument.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCHN_BS);
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!exportNotify.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCHN_BS);
		}
		return result;
	}

	@Override
	public ArrayList<String> submitForm_CM_CCMGBDS(String contactName, String email,
			String fullname, String birthday, String placeOfBirth,
			String national, String idcardNumber, String idcardDate,
			String idcardUnit, String address, String education,
			String certificate, String eduCenter, String eduCenterAddress,String[] fileAttachments, String fileCode) {
		
		ArrayList<String> result = new ArrayList<String>();
		FileCapCCMGBDS fileObj = new FileCapCCMGBDS();
		fileObj.setFileCode(fileCode);
		Set<FileAttachment> lstFile =  new HashSet<FileAttachment>(0);
		if(fileAttachments!=null){
			for (int i = 0; i < fileAttachments.length; i++) {
				String receivedFile = fileAttachments[i];
				if(receivedFile!=null && !"".equals(receivedFile)){
					
					FileAttachment attachfile = createNewAttachFile(receivedFile);
					attachfile.setFileCapCCMGBDS(fileObj);
					lstFile.add(attachfile);
				}
			}
			fileObj.setFileAttachments(lstFile);
		}
		
		fileObj.setThu_tuc(WebConstants.SUBMIT_ONLINE_CAP_CCMGBDS);
		fileObj.setNgay_nop(new Date());
		fileObj.setReceived(false);
		fileObj.setEducation(education);
		fileObj.setAddress(address);
		fileObj.setCertificate(certificate);
		fileObj.setContactName(contactName);
		fileObj.setDateOfBirth(birthday);
		fileObj.setEdu_center_address(eduCenterAddress);
		fileObj.setEdu_center_name(eduCenter);
		fileObj.setEmail(email);
		fileObj.setFullname(fullname);
		fileObj.setIdcard(idcardNumber);
		fileObj.setIdcard_date(idcardDate);
		fileObj.setIdcard_place(idcardUnit);
		fileObj.setNational(national);
		fileObj.setPlaceOfBirth(placeOfBirth);
		fileObj.setType(WebConstants.FORM_TYPE_CM_CCMG);
		
		FileCapCCMGBDS insertedObj = null;
		try {
			
			insertedObj = capCCMGBDSMgr.insert(fileObj);
			
			result.add("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			result.add("FAILURE");
		}
		FileServiceManagerImpl server = (FileServiceManagerImpl) new ClassPathXmlApplicationContext("server.xml").getBean("serverInfo");
		if(insertedObj!=null){
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!getDocument.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCMGBDS_CM);
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!exportNotify.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCMGBDS_CM);
		}
		
		return result;
	}

	@Override
	public ArrayList<String> submitForm_CL_CCMGBDS(String contactName, String email,
			String fullname, String birthday, String placeOfBirth,
			String national, String idcardNumber, String idcardDate,
			String idcardUnit, String address, String education,
			String certificate, String eduCenter, String eduCenterAddress,
			String[] fileAttachments, String fileCode) {
		ArrayList<String> result = new ArrayList<String>();
		FileCapCCMGBDS fileObj = new FileCapCCMGBDS();
		fileObj.setFileCode(fileCode);
		Set<FileAttachment> lstFile =  new HashSet<FileAttachment>(0);
		if(fileAttachments!=null){
			for (int i = 0; i < fileAttachments.length; i++) {
				String receivedFile = fileAttachments[i];
				if(receivedFile!=null && !"".equals(receivedFile)){
					
					FileAttachment attachfile = createNewAttachFile(receivedFile);
					attachfile.setFileCapCCMGBDS(fileObj);
					lstFile.add(attachfile);
				}
			}
			fileObj.setFileAttachments(lstFile);
		}
		
		fileObj.setThu_tuc(WebConstants.SUBMIT_ONLINE_CAPLAI_CCMGBDS);
		fileObj.setNgay_nop(new Date());
		fileObj.setReceived(false);
		fileObj.setEducation(education);
		fileObj.setAddress(address);
		fileObj.setCertificate(certificate);
		fileObj.setContactName(contactName);
		fileObj.setDateOfBirth(birthday);
		fileObj.setEdu_center_address(eduCenterAddress);
		fileObj.setEdu_center_name(eduCenter);
		fileObj.setEmail(email);
		fileObj.setFullname(fullname);
		fileObj.setIdcard(idcardNumber);
		fileObj.setIdcard_date(idcardDate);
		fileObj.setIdcard_place(idcardUnit);
		fileObj.setNational(national);
		fileObj.setPlaceOfBirth(placeOfBirth);
		fileObj.setType(WebConstants.FORM_TYPE_CL_CCMG);
		
		FileCapCCMGBDS insertedObj = null;
		try {
			
			insertedObj = capCCMGBDSMgr.insert(fileObj);
			
			result.add("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			result.add("FAILURE");
		}
		FileServiceManagerImpl server = (FileServiceManagerImpl) new ClassPathXmlApplicationContext("server.xml").getBean("serverInfo");
		if(insertedObj!=null){
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!getDocument.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCMGBDS_CL);
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!exportNotify.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCMGBDS_CL);
		}
		
		return result;
		
	}
	

	@Override
	public ArrayList<String> submitForm_CM_CCDGBDS(String contactName, String email,
			String fullname, String birthday, String placeOfBirth,
			String national, String idcardNumber, String idcardDate,
			String idcardUnit, String address, String education,
			String certificate, String eduCenter, String eduCenterAddress,
			String[] fileAttachments, String fileCode) {
		ArrayList<String> result = new ArrayList<String>();
		FileCapCCMGBDS fileObj = new FileCapCCMGBDS();
		fileObj.setFileCode(fileCode);
		Set<FileAttachment> lstFile =  new HashSet<FileAttachment>(0);
		if(fileAttachments!=null){
			for (int i = 0; i < fileAttachments.length; i++) {
				String receivedFile = fileAttachments[i];
				if(receivedFile!=null && !"".equals(receivedFile)){
					
					FileAttachment attachfile = createNewAttachFile(receivedFile);
					attachfile.setFileCapCCMGBDS(fileObj);
					lstFile.add(attachfile);
				}
			}
			fileObj.setFileAttachments(lstFile);
		}
		
		fileObj.setThu_tuc(WebConstants.SUBMIT_ONLINE_CAP_CCDGBDS);
		fileObj.setNgay_nop(new Date());
		fileObj.setReceived(false);
		
		fileObj.setAddress(address);
		fileObj.setCertificate(certificate);
		fileObj.setContactName(contactName);
		fileObj.setDateOfBirth(birthday);
		fileObj.setEdu_center_address(eduCenterAddress);
		fileObj.setEdu_center_name(eduCenter);
		fileObj.setEmail(email);
		fileObj.setFullname(fullname);
		fileObj.setIdcard(idcardNumber);
		fileObj.setIdcard_date(idcardDate);
		fileObj.setIdcard_place(idcardUnit);
		fileObj.setNational(national);
		fileObj.setPlaceOfBirth(placeOfBirth);
		fileObj.setType(WebConstants.FORM_TYPE_CM_CCDG);
		fileObj.setEducation(education);
		FileCapCCMGBDS insertedObj = null;
		try {
			
			insertedObj = capCCMGBDSMgr.insert(fileObj);
			
			result.add("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			result.add("FAILURE");
		}
		FileServiceManagerImpl server = (FileServiceManagerImpl) new ClassPathXmlApplicationContext("server.xml").getBean("serverInfo");
		if(insertedObj!=null){
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!getDocument.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCDGBDS_CM);
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!exportNotify.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCDGBDS_CM);
		}
		
		return result;
	}

	@Override
	public ArrayList<String> submitForm_CL_CCDGBDS(String contactName, String email,
			String fullname, String birthday, String placeOfBirth,
			String national, String idcardNumber, String idcardDate,
			String idcardUnit, String address, String education,
			String certificate, String eduCenter, String eduCenterAddress,
			String[] fileAttachments, String fileCode) {
		ArrayList<String> result = new ArrayList<String>();
		FileCapCCMGBDS fileObj = new FileCapCCMGBDS();
		fileObj.setFileCode(fileCode);
		Set<FileAttachment> lstFile =  new HashSet<FileAttachment>(0);
		if(fileAttachments!=null){
			for (int i = 0; i < fileAttachments.length; i++) {
				String receivedFile = fileAttachments[i];
				if(receivedFile!=null && !"".equals(receivedFile)){
					
					FileAttachment attachfile = createNewAttachFile(receivedFile);
					attachfile.setFileCapCCMGBDS(fileObj);
					lstFile.add(attachfile);
				}
			}
			fileObj.setFileAttachments(lstFile);
		}
		
		fileObj.setThu_tuc(WebConstants.SUBMIT_ONLINE_CAPLAI_CCDGBDS);
		fileObj.setNgay_nop(new Date());
		fileObj.setReceived(false);
		fileObj.setEducation(education);
		fileObj.setAddress(address);
		fileObj.setCertificate(certificate);
		fileObj.setContactName(contactName);
		fileObj.setDateOfBirth(birthday);
		fileObj.setEdu_center_address(eduCenterAddress);
		fileObj.setEdu_center_name(eduCenter);
		fileObj.setEmail(email);
		fileObj.setFullname(fullname);
		fileObj.setIdcard(idcardNumber);
		fileObj.setIdcard_date(idcardDate);
		fileObj.setIdcard_place(idcardUnit);
		fileObj.setNational(national);
		fileObj.setPlaceOfBirth(placeOfBirth);
		fileObj.setType(WebConstants.FORM_TYPE_CL_CCDG);
		FileCapCCMGBDS insertedObj = null;
		try {
			
			insertedObj = capCCMGBDSMgr.insert(fileObj);
			
			result.add("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			result.add("FAILURE");
		}
		FileServiceManagerImpl server = (FileServiceManagerImpl) new ClassPathXmlApplicationContext("server.xml").getBean("serverInfo");
		if(insertedObj!=null){
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!getDocument.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCDGBDS_CL);
			result.add("http://"+server.getIp()+":"+server.getPort()+"/onegate/exportForm!exportNotify.html?formId="+insertedObj.getId()+"&formtype="+WebConstants.ONLINE_FORM_TYPE_CCDGBDS_CL);
		}
		
		return result;
	}

	@Override
	public List<CategoryBean> loadCategories() {
		List<Category> categories = this.categoryMgr.getByAllowSubmitOnline();
		List<CategoryBean> lstResult = new ArrayList<CategoryBean>();
		if(categories!=null && categories.size()!=0){
			for (int i = 0; i < categories.size(); i++) {
				Category category = categories.get(i);
				
				CategoryBean bean = new CategoryBean();
				bean.setName(category.getCategoryName());
				bean.setValue(category.getId().toString());
				
				lstResult.add(bean);
			}
		}
		
		return lstResult;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getIp() {
		return ip;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getPort() {
		return port;
	}

}
